<?php

namespace App\Filament\Resources;

use App\Filament\Resources\ComponentResource\Pages;
use App\Filament\Resources\ComponentResource\RelationManagers;
use App\Models\Component;
use Filament\Forms\Components\Select;
use Filament\Forms\Form;
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Tables\Table;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Components\Textarea;
use Filament\Forms\Components\Tabs\Tab;
use Filament\Forms\Components\Tabs;
use Wiebenieuwenhuis\FilamentCodeEditor\Components\CodeEditor;
use Filament\Forms\Get;
use App\Models\Category;
use Filament\Forms\Components\TagsInput;
use Filament\Forms\Components\FileUpload;
use Filament\Forms\Components\Section;
use Filament\Tables\Columns\ImageColumn;
use Filament\Tables\Columns\TextColumn;

class ComponentResource extends Resource
{
    protected static ?string $model = Component::class;

    protected static ?string $navigationIcon = 'heroicon-o-rectangle-stack';

    public static function form(Form $form): Form
    {
        return $form
            ->schema([
                    Section::make()->schema([
                        TextInput::make('title')->label('Title'),
                        TextInput::make('description')->label('Description'),
                        Select::make('category')->label('Category')->multiple()->options(
                            Category::all()->pluck('name', 'id')
                        )->required(),
                        FileUpload::make('screenshot')->label('Screenshot')->image(),
                        Select::make('types')->label('Types')->multiple()
                            ->options([
                                'HTML' => 'HTML',
                                'React' => 'React',
                                'Vue' => 'Vue',
                                'WordPress' => 'WordPress',
                                'Livewire' => 'Livewire',
                            ])->required()->live()->default(['HTML']),
                        TagsInput::make('tags')->label('Tags')->placeholder('please press enter after one tag'),
                    ])->columnSpan(1),



                        Tabs::make('Tabs')
                            ->tabs([
                                Tab::make('HTML')
                                    ->schema([
                                        Textarea::make('html_type_description')->label('Type Description'),
                                        CodeEditor::make('html')->label('HTML'),
                                        CodeEditor::make('js')->label('JS'),
                                        CodeEditor::make('css')->label('CSS'),
                                    ]),
                                Tab::make('React')
                                    ->schema([
                                        Textarea::make('react_type_description')->label('Type Description'),
                                        CodeEditor::make('JSX')->label('JSX'),
                                    ])->hidden(fn (Get $get) => !in_array('React', $get('types'))),
                                Tab::make('Vue')
                                    ->schema([
                                        Textarea::make('vue_type_description')->label('Type Description'),
                                        CodeEditor::make('VueJS')->label('VueJS'),
                                    ])->hidden(fn (Get $get) => !in_array('Vue', $get('types'))),

                            ]),
            ])->columns(2);
    }

    public static function table(Table $table): Table
    {
        return $table
            ->columns([
                ImageColumn::make('screenshot'),
                TextColumn::make('title'),
            ])
            ->filters([
                //
            ])
            ->actions([
                Tables\Actions\EditAction::make(),
            ])
            ->bulkActions([
                Tables\Actions\BulkActionGroup::make([
                    Tables\Actions\DeleteBulkAction::make(),
                ]),
            ]);
    }

    public static function getRelations(): array
    {
        return [
            //
        ];
    }

    public static function getPages(): array
    {
        return [
            'index' => Pages\ListComponents::route('/'),
            'create' => Pages\CreateComponent::route('/create'),
            'edit' => Pages\EditComponent::route('/{record}/edit'),
        ];
    }
}
